import json
import numpy as np


global sample_num
config = {'time_url':'E:\PycharmProjects\project\static\json\other_turn_over_time.json',
          'volt_url':'E:\PycharmProjects\project\static\json\other_turn_over_volt.json',
          'fft_time_url':'E:\PycharmProjects\project\static\json\other_turn_over_fft_time.json',
          'fft_volt_url':'E:\PycharmProjects\project\static\json\other_turn_over_fft_volt.json',
          }
#对于每个动作的时间电压做fft
def FFT(time_url,volt_url,fft_time_url,fft_volt_url):
    sample_num = 2048
    with open(time_url, 'r') as time_load_f:
        time_load_dict = json.load(time_load_f)
    with open(volt_url, 'r') as volt_load_f:
        volt_load_dict = json.load(volt_load_f)
    #对于传感器1时间电压做的fft，由于三个传感器时间一样，所以处理一个传感器时间
    x_device=time_load_dict["1"]
    y_device_1=volt_load_dict["1"]
    fft_x_device=x_device[0:sample_num]   #样本点数
    fft_y_device_1= y_device_1[0:sample_num]  #样本点数
    after_fft_x_device=np.arange(len(fft_y_device_1))   #频率
    after_fft_y_device_1=abs(np.fft.fft(fft_y_device_1))/len(fft_x_device)  #快速傅里叶变换,然后做归一化处理
    # 对于传感器2时间电压做的fft
    y_device_2 = volt_load_dict["2"]
    fft_y_device_2 = y_device_2[0:sample_num]  # 样本点数
    after_fft_y_device_2 = abs(np.fft.fft(fft_y_device_2)) / len(fft_x_device)  # 快速傅里叶变换,然后做归一化处理
    # 对于传感器3时间电压做的fft
    y_device_3 = volt_load_dict["3"]
    fft_y_device_3 = y_device_3[0:sample_num]  # 样本点数
    after_fft_y_device_3 = abs(np.fft.fft(fft_y_device_3)) / len(fft_x_device)  # 快速傅里叶变换,然后做归一化处理
    fft_volt={}
    fft_volt["1"]=after_fft_y_device_1.tolist()
    fft_volt["2"] = after_fft_y_device_2.tolist()
    fft_volt["3"] = after_fft_y_device_3.tolist()
    json_volt = json.dumps(fft_volt)
    with open(fft_volt_url,'w') as f_volt:
        f_volt.write(json_volt)
    fft_time={}
    fft_time["time"]= after_fft_x_device.tolist()
    json_time = json.dumps(fft_time)
    with open(fft_time_url, 'w') as f_time:
        f_time.write(json_time)

if __name__=='__main__':
    FFT(time_url=config['time_url'],volt_url=config['volt_url'],fft_time_url=config['fft_time_url'],fft_volt_url=config['fft_volt_url'])